package com.jyx.file.io;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintStream;
import java.io.RandomAccessFile;
import java.util.Scanner;


public class IOTest {

	public static void main(String[] args) throws FileNotFoundException {
		
		
//		randomAccessFileRead();
//		printStrem();
//		toPassworkd();
//		toConvert(5, 3);
	}
	
	/**
	 * 位置调换
	 * @param a
	 * @param b
	 */
	public static void toConvert(int a, int b) {
		System.out.println("a="+a+",b="+b);
		// methodOne
		int temp = a;
		a = b;
		b = temp;
		System.out.println("a="+a+",b="+b);
		// methodTwo
		a = a + b;
		b = a - b;
		a = a - b;
		System.out.println("a="+a+",b="+b);
		// methodThree
		a = a ^ b;
		b = a ^ b;
		a = a ^ b;
		System.out.println("a="+a+",b="+b);
		
		
	}

	/**
	 * 异或^ 加密解密
	 */
	public static void toPassworkd() {
		
		Scanner input = new Scanner(System.in);
		System.out.println("请输入字符串：");
		String passworld = input.nextLine();
		System.out.println("输入的字符串："+passworld);
		char[] charArr = passworld.toCharArray();
		for (int i = 0; i < charArr.length ; i ++) {
			charArr[i] = (char)(charArr[i]^2000);
		}
		System.err.println(new String(charArr));
		for (int i = 0; i < charArr.length ; i ++) {
			charArr[i] = (char)(charArr[i]^2000);
		}
		System.err.println(new String(charArr));
		
	}

	/**
	 * 输出流
	 * @throws FileNotFoundException
	 */
	public static void printStrem() throws FileNotFoundException {
		PrintStream out = System.out;
		PrintStream ps = new PrintStream("d:/logs.txt");
		System.setOut(ps);
		int age = 18;
		System.out.println("年龄变量定义成功，初始值为18");
		String gender = "女";
		System.out.println("性别变量定义成功，初始值为女");
		String info = "这是个"+gender+"孩子，应该"+age+"岁了！";
		System.out.println("整合变量info："+info);
		System.setOut(out); // 回复原有输出流
		System.out.println("程序结束");
	}

	/**
	 * 随机访问
	 */
	public static void randomAccessFileRead(){
		try {
			// 创建文件引用
			File fileWrite = new File("d:" + File.separator + "1.txt");
			// 获取文件 r-只读  w-可编辑 
			RandomAccessFile raf = new RandomAccessFile(fileWrite, "rw");
			String name = null; 
			int age = 0;
			// 1
			name = "tom";
			age = 18;
			raf.writeBytes(name);
			raf.writeInt(age);
			// 2
			name = "mike";
			age = 20;
			raf.writeBytes(name);
			raf.writeInt(age);
			raf.close();

			// 创建文件引用
			File fileRead = new File("d:" + File.separator + "1.txt");
			// 获取文件 r-只读  w-可编辑 
			RandomAccessFile rdf = new RandomAccessFile(fileRead,"r");
			byte [] b = new byte[4];

			rdf.skipBytes(7);//跳过7个字节,即第一个人的信息

			for (int i = 0; i < b.length; i++) 
				b[i] = rdf.readByte();
			name = new String(b);
			age = rdf.readInt();
			System.out.println("第二个人-->姓名:" + name + ";年龄:"+ age);
			rdf.seek(0);// 指针回到文件的开始
			b = new byte[3];
			for (int i = 0; i < b.length; i++) 
				b[i] = rdf.readByte();
			name = new String(b);
			age = rdf.readInt();
			System.out.println("第一个人-->姓名:" + name + ";年龄:"+ age);
			rdf.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
